package com.yanxu.threadandobjectmethods;

/**
 * @author 折戟沉沙铁未销
 * @version V1.0
 * @date 2024/11/10-2024
 * @Description: 演示 wait、notify 相关的方法
 */
public class WaitShow {

    public static Object object_lock = new Object();
    public static void main(String[] args) throws InterruptedException {
        //定义线程1
        Thread thread1 = new Thread(new Runnable() {
            public void run() {
                synchronized (object_lock) {
                    try {
                        System.out.println("thread1 开始执行了！！！");
                        Thread.sleep(5000);
                        object_lock.wait();
                        System.out.println("thread1 继续执行了！！！");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        });

        //定义线程2
        Thread thread2 = new Thread(new Runnable() {
            public void run() {
                synchronized (object_lock) {
                    System.out.println("thread2 获取到了锁, 开始执行了！！！");
                    object_lock.notify();
                    System.out.println("thread2 调用了notiofy 方法之后，continue！！！");
                }
            }
        });


        //启动线程
        thread1.start();
        Thread.sleep(500);
        thread2.start();


    }
}